Ext.define('Admin.view.post.PostViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.postViewController',
    
    openEmployeeWindow: function(view, td, cellIndex, record, tr, rowIndex, e, eOpts){
    	 // var postEmployeeGirdPanel=Ext.getCmp('employeeGridPanel');
      var cfg = Ext.apply({
                xtype: 'postEmployeeAddWindow',
                items: [Ext.apply({xtype: 'postEmployeeGridPanel'})]
    });
    var win = Ext.create(cfg);
    var store =Ext.getCmp('postEmployeeGridPanel').getStore();
    //var store=Ext.data.StoreManager.lookup('employeeStore');
    //var searchEmployeeBypostId=record.data.postId;
   // Ext.Msg.alert('错误', ''+store);
    Ext.apply(store.proxy.extraParams, {postId:record.data.postId});
    store.load({params:{start:0, limit:25, page:1}});
    }
    	
    ,search: function(view, recIndex, cellIndex, item, e, record) {
		
		
		var store = Ext.getCmp('postGridPanel').getStore();
		
		Ext.apply(store.proxy.extraParams, {postName:this.lookupReference('postSearchPostName').getValue()});
		
		store.load({params:{start:0, limit:25, page:1}});
		
    } ,openAddWindow: function(view, recIndex, cellIndex, item, e) {
      		    	var cfg = Ext.apply({
                xtype: 'postAddWindow',
                items: [Ext.apply({xtype: 'postAddForm'})]
		});
		var win = Ext.create(cfg);
    },submitAddForm: function(btn) {
      		var form = btn.up('form');
			form.getForm().submit({       
				url:'post/saveOrUpdate',
				//waitMsg: '正在上传，请耐心等待....',
				success: function(view, action){    
					Ext.Msg.alert('提示', action.result.msg,function(){
						btn.up('window').close();
						//form.getViewModel().getStore('postStore').reload();
	                    Ext.getCmp('postGridPanel').getStore().reload();
					});       
				},       
				failure: function(view, action){
					Ext.Msg.alert('提示', action.result.msg);
				}
			});
    }
    ,openEditWindow: function(view, recIndex, cellIndex, item, e, record) {
    	var cfg = Ext.apply({
                xtype: 'postAddWindow',
                items: [Ext.apply({xtype: 'postEditForm'})]
		});
		var win = Ext.create(cfg);
		
		win.down("form").getForm().loadRecord(record);//加载修改数据到Form
		
      	
    }
     ,submitEditForm: function(btn) {
      		var form = btn.up('form');
			form.getForm().submit({       
				url:'post/saveOrUpdate',
				//waitMsg: '正在上传，请耐心等待....',
				success: function(view, action){    
					Ext.Msg.alert('提示', action.result.msg,function(){
						btn.up('window').close();
						//form.getViewModel().getStore('postStore').reload();
	                    Ext.getCmp('postGridPanel').getStore().reload();
					});       
				},       
				failure: function(view, action){
					Ext.Msg.alert('提示', action.result.msg);
				}
			});
    }
    ,deletePost: function(view, recIndex, cellIndex, item, e, record) {
      		Ext.MessageBox.confirm('提示', '确定要进行删除操作吗？数据将无法还原！',
      			function(btn, text){
                	if(btn=='yes'){
                			Ext.Ajax.request({ 
								url : 'post/delete', 
								method : 'post', 
								params : { 
									postId :record.get('postId')
								}, 
								success: function(response, options) {
					                var json = Ext.util.JSON.decode(response.responseText);
						            if(json.success){
						            	Ext.Msg.alert('系统提示', json.msg, function() {
						                    view.getStore().reload();
						                });
							        }else{
							        	 Ext.Msg.alert('系统提示', json.msg);
							        }
					            }
							});
               		}
            	}
            , this);
    },
    deletePosts: function(btn) {
      	var grid = btn.up('gridpanel');
		var selModel = grid.getSelectionModel();
        if (selModel.hasSelection()) {
            Ext.Msg.confirm("警告", "确定要删除吗？", function (button) {
                if (button == "yes") {
                    var rows = selModel.getSelection();
                    var selectIds = []; //要删除的id
                    Ext.each(rows, function (row) {
                        selectIds.push(row.data.id);
                    });
                  	Ext.Ajax.request({ 
						url : 'post/deletePosts', 
						method : 'post', 
						params : { 
							//ids[] :selectIds
							ids :selectIds
						}, 
						success: function(response, options) {
			                var json = Ext.util.JSON.decode(response.responseText);
				            if(json.success){
				            	Ext.Msg.alert('操作成功', json.msg, function() {
				                    grid.getStore().reload();
				                });
					        }else{
					        	 Ext.Msg.alert('操作失败', json.msg);
					        }
			            }
					});

                }
            });
        }
        else {
            Ext.Msg.alert("错误", "没有任何行被选中，无法进行删除操作！");
        }
    } ,
	postTip:function( view, record, item, index, e, eOpts ){
      if (view.tip == null) {  //这块判断很重要，不能每次都创建个tooltip，要不显示会有问题。
                view.tip = Ext.create('Ext.tip.ToolTip', {
                    // The overall target element.
                    target: view.el,
                    // Each grid row causes its own separate show and hide.
                    delegate: view.itemSelector,
                    // Moving within the row should not hide the tip.
                    //  trackMouse: false,
                    // Render immediately so that tip.body can be referenced prior to the first show.
                    renderTo: Ext.getBody()

                });
            };
           
             view.el.clean();
             view.tip.update("双击查看该岗位的员工");
   }

});